package com.ruoyi.io;
import java.io.BufferedInputStream;   // 引入 BufferedInputStream 类
import java.io.BufferedOutputStream;  // 引入 BufferedOutputStream 类
import java.io.FileInputStream;        // 引入 FileInputStream 类
import java.io.FileOutputStream;       // 引入 FileOutputStream 类
import java.io.IOException;            // 引入 IOException 类
public class BufferedStreamsExample {public static void main(String[] args) {

    String sourceFile = "E:\\source.txt";
    String targetFile = "E:\\target.txt";

    try {
        // 创建 BufferedInputStream 和 BufferedOutputStream 对象
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile));

        // 读取数据，直到读取的内容为-1
        int data;
        while ((data = bufferedInputStream.read()) != -1) {
            bufferedOutputStream.write(data);
        }

        // 关闭 BufferedInputStream 和 BufferedOutputStream 对象
        bufferedInputStream.close();
        bufferedOutputStream.close();

        // 打印成功信息
        System.out.println("复制文件成功！");

    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
